package com.beone.admin.controller.app;

import com.base.common.beans.Result;
import com.beone.admin.controller.app.common.BaseAppController;
import com.beone.admin.entity.Task;
import com.beone.admin.entity.TaskType;
import com.beone.admin.form.TaskTypeForm;
import com.beone.admin.query.common.BaseQuery;
import com.beone.admin.service.TaskTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 任务类型管理
 * </p>
 *
 * @author Pagenny
 * @date 2019-05-19 17:22
 **/
@RestController("appTaskTypeController")
@RequestMapping("/api/taskType")
@Api(value = "APP任务类型", tags = {"APP端"})
public class TaskTypeController extends BaseAppController {

    @Autowired
    private TaskTypeService taskTypeService;

    @ApiOperation(value = "任务类型列表", notes = "查询当前用户的任务类型列表")
    @RequestMapping(value = "/list", method = {RequestMethod.GET})
    public Object list(BaseQuery query) {
        return taskTypeService.getTaskTypePagination(getUserId(), query.getPage(), query.getRows());
    }

    @ApiOperation(value = "保存任务类型", notes = "保存任务类型 主键属性为空新增,否则修改")
    @PostMapping(value = "/save")
    public Result save(TaskTypeForm form) {
        form.setUserId(getUserId());
        if(taskTypeService.insertOrUpdate(form)){
            return Result.success();
        }
        return Result.failure("保存失败");
    }

    @ApiOperation(value = "删除任务类型", notes = "根据id删除任务类型")
    @PostMapping(value = "/delete")
    @ApiImplicitParam(name = "typeId", value = "类型id", required = true, paramType = "param", dataType = "string")
    public Result delete(Integer typeId) {
        TaskType type = taskTypeService.selectById(typeId);
        if (type == null) {
            return Result.failure("没有找到指定任务类型");
        }
        if (!getUserId().equals(type.getCreateUserId())) {
            return Result.failure("没有权限操作");
        }
        return Result.success(taskTypeService.deleteById(typeId));
    }
}
